package animix.blue.server.user.controller;

import animix.blue.server.common.bean.pojo.LogLoginAnno;
import animix.blue.server.common.bean.pojo.LogOperationAnno;
import animix.blue.server.common.bean.tool.LoginBean;
import animix.blue.server.common.bean.tool.LogoutBean;
import animix.blue.server.common.bean.pojo.Result;
import animix.blue.server.common.bean.model.User;
import animix.blue.server.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(description = "用户管理API")
public class UserController {

    @Autowired
    private UserService userService;

    @LogLoginAnno
    @ApiOperation(value = "登陆", notes = "用户登陆")
    @PostMapping("/user/login")
    public Result login(@RequestBody LoginBean loginBean) {
        if (loginBean != null && loginBean.getUsername() != null && loginBean.getPassword() != null) {
            return userService.login(loginBean.getUsername(), loginBean.getPassword(), loginBean.getRemember());
        } else {
            return Result.build(400, "请求参数无效");
        }
    }

    @ApiOperation(value = "获取用户信息", notes = "根据token获取用户信息")
    @ApiImplicitParam(name = "token", value = "Token", required = true, dataType = "String")
    @GetMapping("/user/info/{token}")
    public Result getInfoByToken(@PathVariable String token) {
        return userService.getInfoByToken(token);
    }

    @ApiOperation(value = "登出", notes = "用户登出")
    @PostMapping("/user/logout")
    public Result logout(@RequestBody LogoutBean logoutBean) {
        if (logoutBean != null && logoutBean.getToken() != null) {
            return userService.logout(logoutBean.getToken());
        } else {
            return Result.build(400, "请求参数无效");
        }
    }

    @ApiOperation(value = "查询用户", notes = "根据条件查询，返回用户列表")
    @GetMapping("/user")
    public Result list(@RequestParam Map<String, Object> para) {
        return userService.list(para);
    }

    @ApiOperation(value = "获取用户", notes = "根据id获取用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Int")
    @GetMapping("/user/{id}")
    public Result getById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @LogOperationAnno(action="新增", object="用户")
    @ApiOperation(value = "添加用户", notes = "根据User对象创建用户")
    @PostMapping("/user")
    public Result add(@RequestBody User user) {
        return userService.add(user);
    }

    @LogOperationAnno(action="修改", object="用户")
    @ApiOperation(value = "修改用户", notes = "根据User对象（Id）修改用户信息")
    @PutMapping("/user")
    public Result update(@RequestBody User user) {
        return userService.updateById(user);
    }

    @LogOperationAnno(action="删除", object="用户")
    @ApiOperation(value = "删除用户", notes = "根据Id数组删除用户")
    @DeleteMapping("/user")
    public Result delete(@RequestBody Map<String, Object> para) {
        Integer[] ids = getIdsFromMap(para);
        return userService.deleteByIds(ids);
    }

    @LogOperationAnno(action="删除", object="用户")
    @ApiOperation(value = "删除用户", notes = "根据Id删除用户")
    @DeleteMapping("/user/{id}")
    public Result delete(@PathVariable Integer id) {
        return userService.deleteById(id);
    }

    @LogOperationAnno(action="重置密码", object="用户")
    @ApiOperation(value = "重置用户密码", notes = "根据Id数组重置用户密码")
    @PutMapping("/user/resetpwd")
    public Result resetPwd(@RequestBody Map<String, Object> para) {
        Integer[] ids = getIdsFromMap(para);
        return userService.resetPwdByIds(ids);
    }

    @LogOperationAnno(action="禁用", object="用户")
    @ApiOperation(value = "禁用用户", notes = "根据Id数组禁用用户")
    @PutMapping("/user/disable")
    public Result disable(@RequestBody Map<String, Object> para) {
        Integer[] ids = getIdsFromMap(para);
        return userService.disableByIds(ids);
    }

    private Integer[] getIdsFromMap(Map<String, Object> para) {
        List idList = (List<Integer>) para.get("ids");
        if (idList != null && idList.size() > 0) {
            Integer[] ids = new Integer[idList.size()];
            for (int i = 0; i < idList.size(); i++) {
                ids[i] = (Integer) idList.get(i);
            }
            return ids;
        } else {
            return null;
        }
    }
}